import argparse
from download.package_download import PackageDonwloader
from parse.parser import Parser

from pathlib import Path

class Crawler(object):
    def __init__(self,
                 file_path : str,
                 pkg_type : str,
                 csv_sep : str,
                 last_sync : str,
                 has_header : bool = False,
                 log_dir : str = "/tmp"):

        assert Path(file_path).exists()
        opts = { 'header' : None } if has_header else {}
        self.parser = Parser(file_path, pkg_type, csv_sep, opts)
        self.parser.set_begin_point(last_sync)
        self.downloader = PackageDonwloader(pkg_type, log_dir)

    def start_crawl(self):
        for pkg in self.parser.coordinates_list():
            self.downloader.add_task(pkg)
        self.downloader.wait()

def main():
    parser = argparse.ArgumentParser(description='Sync packages according to csv file')

    parser.add_argument('-f', '--file', required=True,
                        help='specify the csv file which contain package info to sync')
    parser.add_argument('-t', '--type', required=True,
                        help='specify the type of packages ready to sync')
    parser.add_argument('-s', '--sep', default=" ",
                        help='specify the sep of csv file')
    parser.add_argument('--no-header', action='store_true',
                        help='whether the csv file has header')
    parser.add_argument('-o', '--log-dir', default="/tmp",
                        help='specify log directory')
    parser.add_argument('--last-sync', default=None,
                        help='specify last sync package')
    args = parser.parse_args()

    if Path(args.file).exists():
        Crawler(args.file,
                args.type,
                args.sep,
                args.last_sync,
                args.no_header,
                args.log_dir
        ).start_crawl()
    else:
        print(f"[error] no such file: {args.file}")
        parser.print_help()




if __name__ == "__main__":
    main()
